import GlobalData from "../GlobalData";

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

import HttpUtils from "../../libs/lightMVC/util/HttpUtils";
import itemmove from "../itemmove";
import Socket from "../Socket";

@ccclass
export default class ContentScrollView extends cc.Component {

    @property(cc.ScrollView)
    scroll_view: cc.Node = null;
    
    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Prefab)
    anim_prefab: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    //示例化socket
    @property(Socket)
    socket:Socket = null;

    start () {

        let resultValue = this.randResult();
        // console.log(resultValue);

        let _this = this;
        setTimeout(function () {

           _this.socket.sendMessage();

        },6000)

        let page = 1;

        this.add_item_content(page);

        let interval =setInterval(()=>{

            if(page >= 25){

                clearInterval(interval);
 
             }else{
                 page++;
             }

            let r = this.add_item_content(page);
           
        },3000);
    }

    //添加数据到scrollView
    add_item_content(page){
        let t_url = 'http://platform.qdmedia.cc/activity/space7_item';
        HttpUtils.Post(t_url,{page:page},(error, result)=>{

            if(result && !error){
                let r = JSON.parse(result);
                // console.log(r)
                GlobalData.gameDatas = r;
                let data = GlobalData.gameDatas;
                let length = GlobalData.gameDatas.length;
            
                //添加数据
                for(var i=0;i<length;i++){
    
                    let item = cc.instantiate(this.anim_prefab);
                
                    this.content.addChild(item);

                    //y高度范围 -200~-800, x范围 1700~1900
                    let y = Math.random() * 600 + 150, x=Math.random() * 200 + 1700
                    // item.y = -y;
                    if(page===1){
                        item.x = 800;
                    }else if(page===2){
                        item.x = 1100;
                    }else if(page===3){
                        item.x = 1550;
                    }else{                        
                        item.x = 2300;
                    }
                    switch(i){
                        case 1:
                            item.y =  -(Math.random() * 100 + 150);
                            break;                            
                        case 2:
                            item.y =  -(Math.random() * 100 + 300);
                            break;                            
                        case 3:                                
                            item.y =  -(Math.random() * 100 + 450);
                            break;                            
                        case 4:
                            item.y =  -(Math.random() * 100 + 600);
                            break;
                        default:
                            item.y =  -(Math.random() * 600 + 150);
                            break;          
                    }
                    // console.log(i,page,item.x,item.y);
                    item.getComponent(itemmove).init();            

                    // data[i].id + '.' +  序号
                    item.getComponent(cc.Label).string = data[i].title;
                    //添加属性
                    var attr = {id:data[i].id, title:data[i].title}
                    item.attr(attr);

                } 
            }else{
                return 'error';
            }
           
        }); 
    }


    /**
     * 获取结果页数据
     */
    randResult() {
        
        let list = [
            {'key':1,'value':'春暖花开，有份姻缘在莫奈花园等你赴约','title':'脱单'},
            {'key':2,'value':'努力是会上瘾的，成功的甜头就藏在七大洲里属于你的一隅','title':'脱单'},
            {'key':3,'value':'挨过三冬四夏，登顶后看看莫奈花园的无限风光','title':'升职'},
            {'key':4,'value':'理想生活化作清风拨动透气7音符，传出怡然的乐章向你袭来','title':'安康'},
            {'key':5,'value':'坚持努力后的等价收获在约你览尽七大洲山河浪漫','title':'加薪'},
            {'key':6,'value':'愿你言出意随，伴着美妙的7音符奔赴天然之约','title':'如意'},
            {'key':7,'value':'世界上最好的保鲜是不断进步，继而成为七大洲中不可或缺的风景线','title':'暴瘦'},
        ]

        let k = Math.floor(Math.random() * 6 + 1);

        return list[k];
        
    }

    // update (dt) {}
}
